package com.dmgameclient.common;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;

/**
 * @name SDCard二级缓存
 * @Descripation <br>
 *               1、写入一张图片(Bitmap,byte[])到SDCard指定路径。<br>
 *               2、从指定路径读取一张图片(Bitmap,byte[])。<br>
 *               3、判断是否存在图片文件。<br>
 * @author 樊俊彬
 * @date 2014-8-10
 * @version 1.0
 */

public class SDCardCache {

	private static final String TAG = "SDCardCache";

	/**
	 * SDCard是否可用
	 * 
	 * @return boolean
	 */
	private boolean isSDCardAvailiable() {
		return Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED);
	}

	/**
	 * 图片是否存在
	 * 
	 * @param fileName
	 * @param folder
	 * @return boolean
	 */
	public boolean isFileExist(String fileName, String folder) {
		if (isSDCardAvailiable() && fileName != null) {
			File tagetDir = new File(Environment.getExternalStorageDirectory(),
					folder);
			if (!tagetDir.exists()) {
				return false;
			}
			File tagetFile = new File(tagetDir, fileName);
			if (tagetFile.isFile()) {
				return true;
			} else {
				return false;
			}
		}
		return false;
	}

	/**
	 * 写入一张图片(byte[])到SDCard指定路径
	 * 
	 * @param fileName
	 * @param folder
	 * @param data
	 * @return boolean
	 */
	public String writeImageByPath(String folder, String fileName, byte[] data) {
		if (isSDCardAvailiable() && !isFileExist(fileName, folder)) {
			File targetDir = new File(
					Environment.getExternalStorageDirectory(), folder);
			if (!targetDir.exists()) {
				targetDir.mkdirs();
			}

			File targetFile = new File(targetDir, fileName);
			if (!isFileExist(fileName, folder)) {
				FileOutputStream fileOutputStream = null;
				try {
					fileOutputStream = new FileOutputStream(targetFile);
					fileOutputStream.write(data);
					return targetFile.getAbsolutePath();
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
					if (fileOutputStream != null) {
						try {
							fileOutputStream.close();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}
		}
		return null;
	}

	/**
	 * 从指定路径读取一张图片(byte[])
	 * 
	 * @param fileName
	 * @param folder
	 * @return Bitmap
	 */
	public byte[] readImageByPath(String fileName, String folder) {
		if (isFileExist(fileName, folder)) {
			File targetDir = new File(
					Environment.getExternalStorageDirectory(), folder);
			File targetFile = new File(targetDir, fileName);
			FileInputStream fileInputStream = null;
			ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

			try {
				fileInputStream = new FileInputStream(targetFile);
				int len = 0;
				byte[] buffer = new byte[2048];
				while ((len = fileInputStream.read(buffer)) != -1) {
					byteArrayOutputStream.write(buffer, 0, len);
				}
				// Log.i(TAG, ">>>文件读取成功" + targetFile.getAbsolutePath());
				return byteArrayOutputStream.toByteArray();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				if (fileInputStream != null) {
					try {
						fileInputStream.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

				if (byteArrayOutputStream != null) {
					try {
						byteArrayOutputStream.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
		// Log.i(TAG, ">>>文件读取失败");
		return null;
	}

	/**
	 * 从指定路径读取一张图片(Bitmap)
	 * 
	 * @param fileName
	 * @param folder
	 * @return Bitmap
	 */
	public Bitmap readBitmapByPath(String fileName, String folder) {
		byte[] data = readImageByPath(fileName, folder);
		if (data != null) {
			return BitmapFactory.decodeByteArray(data, 0, data.length);
		}
		return null;
	}
}